package com.longrise.android.byjk.plugins.tabfirst.allfunction;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.common.utils.AppUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by callxkj on 2017/3/8.
 * 全部功能的rcvadapter
 */

public class AllFunctionRcvAdapter extends BaseQuickAdapter<EntityBean, BaseViewHolder> {

    List<EntityBean> mBeanlist = new ArrayList<>();
    private OnAllFunctionItemClickListener mListener;

    public AllFunctionRcvAdapter() {
        super(R.layout.rcv_allfunction, null);
    }

    public void setData(EntityBean[] beans) {
        if (beans == null) {
            return;
        }
        mBeanlist.clear();
        for (EntityBean bean : beans) {
            mBeanlist.add(bean);
        }
        setNewData(mBeanlist);
    }

    @Override
    protected void convert(BaseViewHolder helper, EntityBean item) {
        String backgroundurl = item.getString("backgroundurl");
        EntityBean[] childmenuses = item.getBeans("childmenus");
        if (childmenuses.length == 0) {
            return;
        }

        View convertView = helper.getConvertView();
        RecyclerView rcv_item = (RecyclerView) convertView.findViewById(R.id.allfunction_item_rcv);

        reDefineRcvHeight(rcv_item, childmenuses);

        ImageView iv_bg = (ImageView) convertView.findViewById(R.id.allfunction_item_iv);
        Glide.with(AppUtil.getContext()).load(backgroundurl).into(iv_bg);
        AllFunctionRcvItem1Adapter adapter = new AllFunctionRcvItem1Adapter();
        adapter.setData(childmenuses);
        rcv_item.setLayoutManager(new GridLayoutManager(mContext, 4));
        rcv_item.setAdapter(adapter);

        adapter.setOnAllFunctionItem1ClickListener(new AllFunctionRcvItem1Adapter.OnAllFunctionItem1ClickListener() {
            @Override
            public void onClick(String uniqueid, String linkedtype, String showname, String linkedurl, String fullurl) {
                if(mListener!=null){
                    mListener.onClick(uniqueid, linkedtype, showname, linkedurl, fullurl);
                }
            }
        });
    }

    /**
     * 重新定义rcv的高度
     */
    private void reDefineRcvHeight(RecyclerView rcv, EntityBean[] beans) {
        ViewGroup.LayoutParams layoutParams = rcv.getLayoutParams();
        int width = AppUtil.getScreenWidth();
        int itemHeight = width / 4;
        int num1 = beans.length / 4;
        int num2 = beans.length % 4;
        if(num2>0){
            layoutParams.height = itemHeight*(num1+1);
        }else{
            layoutParams.height = itemHeight*num1;
        }
        rcv.setLayoutParams(layoutParams);
    }

    /**
     * 设置每个button的监听
     */
    public void setOnAllFunctionItemClickListener(OnAllFunctionItemClickListener listener) {
        this.mListener = listener;
    }

    public interface OnAllFunctionItemClickListener {
        void onClick(String uniqueid, String linkedtype, String showname, String linkedurl,
                     String fullurl);
    }
}
